let loginFormRules = {
  userName: [{
    type: 'reg',
    value: /\S+/ig,
    msg: '请输入用户名'
  }],
  password: [{
    type: 'reg',
    value: /\S+/ig,
    msg: '请输入密码'
  }]
};
let signupFormRules = {
  userName: [{
    type: 'reg',
    value: /\S+/ig,
    msg: '请输入用户名'
  }],
  password: [{
    type: 'reg',
    value: /\S+/ig,
    msg: '请输入密码'
  }],
  confirmPwd: [{
    type: 'reg',
    value: /\S+/ig,
    msg: '请再次输入密码'
  }, {
    type: 'equal',
    target: 'password',
    msg: '两次密码输入不一致'
  }]
};

var checkFunc = {
    // maxLength
    maxLength: function(max, value, msg) {
        var bValid = true;
        if (value.length > max) {
            bValid = false;
        }
        return {
            bValid: bValid,
            msg: msg
        };
    },
    reg: function(regStr, value, msgs) {
        var bValid = true;
        var pattern = regStr,
            msg = msgs;
        //msgs = msgs.split('#');
        var regexp = new RegExp(pattern);
        if (!regexp.test(value)) {
            bValid = false;
            return {
                bValid: bValid,
                msg: msg
            };
        }

        return { bValid: true };
    },
    moreThan: function(ruleValue, value, msg, target) {
        var bValid = true;
        if (parseFloat(value, 10) > parseFloat(target, 10)) {
            bValid = false;
        }
        return {
            bValid: bValid,
            msg: msg
        };
    },
    /**
     * 当其他值等于默认value时，直接跳过该字段的比较
     * @param  {[type]} ruleValue [description]
     * @param  {[type]} value     [description]
     * @param  {[type]} msg       [description]
     * @param  {[type]} target    [description]
     * @return {[type]}           [description]
     */
    equalBreak: function(ruleValue, value, msg, target) {
        var bValid = true;
        let isCrossOver = false;
        if (parseInt(target, 10) !== ruleValue) {
            bValid = false;
        } else {
            isCrossOver = true;
        }

        return {
            bValid: bValid,
            isCrossOver: isCrossOver,
            msg: msg
        };
    },
    equal: function(ruleValue, value, targetValue) {
      return {
        bValid: value === targetValue
      };
    },
    required:function(value){
        return (value !== null && value !== '' && value !== -1);
    },
    selected:function(ruleValue,value,msg){
        return {
            bValid: value,
            msg: msg
        }
    }
};


function check(info, validateRules) {
	for(var key in validateRules) {
		let rules = validateRules[key];
		let rule;
		for (let i=0; i<rules.length; i++) {
			rule = rules[i];
			let validObj = checkFunc[rule.type](rule.value, info[key], info[rule.target]);
        if (validObj.isCrossOver) {
          i = rules.length;
          break;
        }
			if(validObj.bValid === false && typeof validObj.isCrossOver === 'undefined') {
        validObj.validKey = key;
        validObj.msg = rule.msg;
				return validObj;
			}
		}
	}
	return {
		bValid: true,
		msg: ''
	};
}

/**
 * 校验登录表单
 * @param  {Object} data 表单数据
 * @param  {String} data.userName 用户名
 * @param  {String} data.password 密码(必须经过md5)
 * @return {[type]}      [description]
 */
export function validateLoginForm(data) {
  return check(data, loginFormRules);
}

export function validateSingUpForm(data) {
  return check(data, signupFormRules);
}
